package com.test.project.system.wenjuan.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.common.exception.BusinessException;
import com.test.common.utils.poi.ExcelUtil;
import com.test.framework.web.controller.BaseController;
import com.test.framework.web.domain.AjaxResult;
import com.test.framework.web.page.TableDataInfo;
import com.test.project.system.dajuan.domain.TDajuan;
import com.test.project.system.dajuan.service.ITDajuanService;
import com.test.project.system.dati.domain.TDati;
import com.test.project.system.dati.service.ITDatiService;
import com.test.project.system.shiti.domain.TShiti;
import com.test.project.system.shiti.service.ITShitiService;
import com.test.project.system.user.domain.User;
import com.test.project.system.wenjuan.domain.TWenjuan;
import com.test.project.system.wenjuan.service.ITWenjuanService;

/**
 * 竞赛管理Controller
 * 
 * @author test
 * @date 2023-12-05
 */
@Controller
@RequestMapping("/system/wenjuan")
public class TWenjuanController extends BaseController
{
    private String prefix = "system/wenjuan";
    @Autowired
    private ITDajuanService tDajuanService;

    @Autowired
    private ITShitiService itShitiService;
    @Autowired
    private ITWenjuanService tWenjuanService;

    @Autowired
    private ITDatiService tDatiService;
    @GetMapping()
    public String wenjuan()
    {
        return prefix + "/wenjuan";
    }
    @GetMapping("static")
    public String static1(ModelMap mmap)
    {
    	Map<String,Integer> coutMap = new HashMap<>();
    	List<TDajuan> dajuans = tDajuanService.selectTDajuanList(new TDajuan());
    	for(TDajuan d : dajuans) {
    		User user = d.getUser();
    		if(user == null) {
    			continue;
    		}   
    	}
    	List<String> names = new ArrayList<String>();
    	List<String> values = new ArrayList<String>();
    	coutMap.forEach((k,v)->{
    		names.add(k);
    		values.add(v+"");
    	});
    	mmap.put("names", names);
    	mmap.put("values", values);
        return prefix + "/statistic";
    }
    @GetMapping("statistic1")
    public String statistic1(ModelMap mmap)
    {
 
    	return prefix + "/statistic1";
    }
    /**
     * 查询竞赛管理列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TWenjuan tWenjuan)
    {
        startPage();
        if(getSysUser().getRoleId() == 3L) {
        	tWenjuan.setMyId(getUserId());
        }
        List<TWenjuan> list = tWenjuanService.selectTWenjuanList(tWenjuan);
        return getDataTable(list);
    }

    /**
     * 导出竞赛管理列表
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TWenjuan tWenjuan)
    {
        List<TWenjuan> list = tWenjuanService.selectTWenjuanList(tWenjuan);
        ExcelUtil<TWenjuan> util = new ExcelUtil<TWenjuan>(TWenjuan.class);
        return util.exportExcel(list, "wenjuan");
    }

    /**
     * 新增竞赛管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存竞赛管理
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TWenjuan tWenjuan)
    {
        return toAjax(tWenjuanService.insertTWenjuan(tWenjuan));
    }

    /**
     * 修改竞赛管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TWenjuan tWenjuan = tWenjuanService.selectTWenjuanById(id);
        mmap.put("tWenjuan", tWenjuan);
        return prefix + "/edit";
    }

    /**
     * 修改保存竞赛管理
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TWenjuan tWenjuan)
    {
        return toAjax(tWenjuanService.updateTWenjuan(tWenjuan));
    }

    @GetMapping("/show/{postId}")
    public String show(@PathVariable("postId") Long postId, ModelMap mmap)
    {
		TWenjuan post = tWenjuanService.selectTWenjuanById(postId);
    	mmap.put("post", post);
    	TShiti tTimu = new TShiti();
    	tTimu.setSectionId(postId);
    	List<TShiti> timus = itShitiService.selectTShitiList(tTimu);
    	mmap.put("timus", timus);
    	TDajuan tDajuan = new TDajuan();
    	tDajuan.setWenjuanId(postId);
    	tDajuan.setCreateBy(getLoginName());
    	List<TDajuan> selectTDajuanList = tDajuanService.selectTDajuanList(tDajuan);
    	if(selectTDajuanList.size()>0) {
    		TDajuan dajuan = selectTDajuanList.get(0);
    		if(dajuan.getStatus().equals("0")) {
    			if(post.getShijian().getTime()>System.currentTimeMillis()) {
    				throw new BusinessException("竞赛未到开始时间！");
    			}
    			if(post.getStatus().equals("2")) {
    				throw new BusinessException("竞赛已结束！");
    			}
    			if(post.getStatus().equals("0")) {
    				throw new BusinessException("竞赛未开始！");
    			}
    			return prefix + "/show";
    		}else {
    			TDati tDati = new TDati();
        		tDati.setDajuanId(dajuan.getId());
        		List<TDati> datis = tDatiService.selectTDatiList(tDati);
        		mmap.put("datis", datis);
        		return prefix + "/show1";
    		}
    		
    	}else {
    		return prefix + "/show";
    	}
    	
    }
    /**
     * 删除竞赛管理
     */
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tWenjuanService.deleteTWenjuanByIds(ids));
    }
}
